//
//  SLCityViewController.m
//  futureLive
//
//  Created by zhoubiwen on 15/11/19.
//  Copyright © 2015年 ZHOU_LI_SUN. All rights reserved.
//

#import "SLCityViewController.h"
#import "UIView+Frame.h"
@interface SLCityViewController ()<UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate>
@property (strong, nonatomic) IBOutlet UITableView *tableView;

@property (assign, nonatomic) CGPoint panGesturePoint; //  拖动手势在tableView的落点(记录当前位置的上一个位置，用来与当前位置比较，得出视图)
@property (assign, nonatomic) CGPoint tableViewCenter; // 视图加载后，不做任何操作时的中心点位置

@property (strong, nonatomic) UIPanGestureRecognizer *panGesture;
@end

@implementation SLCityViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    [self initData];
    [self initSubViews];
}
/**
 *  初始化数据
 */
- (void)initData {
    [self.view layoutIfNeeded];
    
    self.tableView.delegate   = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
//    self.tableViewCenter = self.tableView.center;
//    NSLog(@"%@",NSStringFromCGPoint(self.tableViewCenter));
}
/**
 *  初始化子视图
 */
- (void)initSubViews {
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, self.tableView.frame.size.height / 2)];
    view.backgroundColor = [UIColor clearColor];
    self.tableView.tableHeaderView = view;
    
    self.panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGestureAction:)];
    self.panGesture.delegate = self;
    [self.tableView addGestureRecognizer:self.panGesture];
}

- (void)panGestureAction:(UIPanGestureRecognizer *)panGesture {


    CGPoint point = [panGesture translationInView:panGesture.view];
    CGFloat heightInterval = point.y - self.panGesturePoint.y;
    CGPoint contentOffset = self.tableView.contentOffset;

    if (heightInterval < 0 || contentOffset.y > 0) {
        contentOffset.y -= heightInterval;
        self.tableView.contentOffset = contentOffset;
    }
    self.panGesturePoint = point;
    if (panGesture.state  == UIGestureRecognizerStateEnded) {
        if (contentOffset.y < 150) {
            [self.tableView setContentOffset:CGPointZero animated:YES];
        }else if (contentOffset.y >= 150 && contentOffset.y < self.tableView.height / 2){
            
            [self.tableView setContentOffset:CGPointMake(0, self.tableView.height / 2) animated:YES];
        }
        self.panGesturePoint = CGPointZero;
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.translucent = NO;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.translucent = YES;
}

#pragma mark - UIGestureRecognizerDelegate
// 多个手势同时生效
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    
    return YES;
    
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 50;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.textLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row];
    
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    // Dispose of any resources that can be recreated.
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
   
    if (scrollView.contentOffset.y > self.tableView.height / 2) {
        if ([self.tableView.gestureRecognizers containsObject:self.panGesture]) {
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                [self.tableView removeGestureRecognizer:self.panGesture];
            });
        }
    }else {
        if (![self.tableView.gestureRecognizers containsObject:self.panGesture]) {
            [self.tableView addGestureRecognizer:self.panGesture];
            [self panGestureAction:self.panGesture];
        }
    }
    if (scrollView.contentOffset.y == self.tableView.height / 2) {
  }
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {

    NSLog(@"%s,%d",__FUNCTION__,__LINE__);

}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
